package com.ruoyi.common.flowable;

import cn.hutool.extra.spring.SpringUtil;
import com.ruoyi.common.constant.BusinessConstant;
import com.ruoyi.common.flowable.execution.MyFlowableEventListener;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.flowable.engine.impl.persistence.entity.HistoricProcessInstanceEntityImpl;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 任务节点前置监听处理类
 * @author: Lu Yang
 * @create: 2019-05-04 20:51
 **/
@Component
public class ProcessFullListener implements FlowableEventListener {

    public HistoryService getHistoryService() {
        return SpringUtils.getBean(HistoryService.class);
    }

    @Override
    public void onEvent(FlowableEvent event) {

        if(event.getType().equals(FlowableEngineEventType.TASK_COMPLETED)){
            TaskEntity taskEntity = (TaskEntity) ((FlowableEntityEventImpl) event).getEntity();
            TaskCompletedListener taskCreatedListener = SpringUtil.getBean(TaskCompletedListener.class);
            taskCreatedListener.onEvent(event);

            callListener(event,taskEntity.getProcessInstanceId());
        }else if(event.getType().equals(FlowableEngineEventType.TASK_CREATED)){
            TaskEntity taskEntity = (TaskEntity) ((org.flowable.common.engine.impl.event.FlowableEntityEventImpl) event).getEntity();

            TaskCreatedListener taskCreatedListener = SpringUtil.getBean(TaskCreatedListener.class);
            taskCreatedListener.onEvent(event);

            callListener(event,taskEntity.getProcessInstanceId());
        }else if(event.getType().equals(FlowableEngineEventType.PROCESS_COMPLETED)){
            ExecutionEntityImpl taskEntity = (ExecutionEntityImpl) ((FlowableEntityEventImpl) event).getEntity();
            callListener(event,taskEntity.getProcessInstanceId());
        }else if(event.getType().equals(FlowableEngineEventType.HISTORIC_PROCESS_INSTANCE_ENDED)){
            HistoricProcessInstanceEntityImpl taskEntity = (HistoricProcessInstanceEntityImpl) ((FlowableEntityEventImpl) event).getEntity();
            callListener(event,taskEntity.getProcessInstanceId());
        }
        System.out.println(event.getClass());
        System.out.println(event.getType());
    }

    private void callListener(FlowableEvent event,String processInstanceId){
        HistoricProcessInstance historicProcessInstance = getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        String processDefinitionKey = historicProcessInstance.getProcessDefinitionKey();
        if(BusinessConstant.FlowType.EXPERT_FREEZE.equals(processDefinitionKey)){
            processDefinitionKey = "BatchBusinessListener";
        }
        MyFlowableEventListener flowableEventListener = SpringUtils.getBean(processDefinitionKey);
        if (flowableEventListener != null){
            flowableEventListener.onEvent(event);
        }
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }
}